package com.run.guifeng.controller.admin;

import com.run.guifeng.model.entity.Carousel;
import com.run.guifeng.model.enums.ServiceResultEnum;
import com.run.guifeng.service.CarouselService;
import com.run.guifeng.util.Result;
import com.run.guifeng.util.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;


/**
 * @author sky
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class CarouselController {

    @Resource
    CarouselService carouselService;


    /**
     * 列表
     */
    @GetMapping("/carousels/list")
    @ApiOperation("Get a page carousels")
    public Result list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("current")) || StringUtils.isEmpty(params.get("size"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        return ResultGenerator.genSuccessResult(carouselService.getCarouselPage(
                Integer.parseInt(params.get("current").toString()),
                Integer.parseInt(params.get("size").toString())));
    }

    /**
     * 添加
     */
    @PostMapping("/carousels/save")
    @ApiOperation("Save carousel")
    public Result save(@RequestBody Carousel carousel) {
        if (StringUtils.isEmpty(carousel.getCarouselUrl())
                || Objects.isNull(carousel.getCarouselRank())) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result = carouselService.saveCarousel(carousel);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }


    /**
     * 修改
     */
    @PostMapping("/carousels/update")
    @ApiOperation("Update carousels")
    public Result update(@RequestBody Carousel carousel) {
        if (Objects.isNull(carousel.getId())
                || StringUtils.isEmpty(carousel.getCarouselUrl())
                || Objects.isNull(carousel.getCarouselRank())) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result = carouselService.updateCarousel(carousel);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 详情
     */
    @GetMapping("/carousels/info/{id}")
    @ApiOperation("Get carousel by id")
    public Result info(@PathVariable("id") Integer id) {
        Carousel carousel = carouselService.getCarouselById(id);
        if (carousel == null) {
            return ResultGenerator.genFailResult(ServiceResultEnum.DATA_NOT_EXIST.getResult());
        }
        return ResultGenerator.genSuccessResult(carousel);
    }

    /**
     * 删除
     */
    @PostMapping(value = "/carousels/delete")
    @ApiOperation("Delete carousels by ids")
    public Result delete(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (carouselService.deleteBatch(ids).equals("success")) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("删除失败");
        }
    }

}